/**
 * Created by ss on 16/4/20.
 */
var ID = '320eba40dbe5cafb7678da8ded099afb';
var CONSTANT = {
    id: ID
};
var qs = require('querystring');
var http = require("http");
var Promise = require("bluebird");


module.exports = {
    options: {
        hostname: 'http://api.kuaidi.com',
        port: 80,
        method: 'GET'
    },
    httpGet: function (url, data, cb) {
        var getData = Object.assign({}, data);
        var chunkData = "";
        if(!cb) {
            cb = function () {

            }
        }
        return new Promise(function (resolve, reject) {
            var req = http.get(url + "?" + qs.stringify(getData), function (res) {
                res.setEncoding('utf8');
                res.on('data', function (chunk) {
                    chunkData += chunk;
                });
                res.on('end', function () {
                    return resolve(chunkData);
                });
            }).on('error', reject);
        })
    },
    getList: function (data, cb) {
        var get = Object.assign({}, CONSTANT, data);
        return this.httpGet('http://api.kuaidi.com/openapi.html', get, cb);
    },
    getItem: function (data, cb) {
        var get = Object.assign({}, CONSTANT, data);
        return this.httpGet('http://www.kuaidi.com/index-ajaxselectcourierinfo-'+data.nu+'-'+data.com+'.html', get, cb);
    }
}